<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Model\SmsMsg;
use Sts\AssumeRoleRequest;
use Sts\Core\DefaultAcsClient;
use Sts\Core\Profile\DefaultProfile;

class OssController extends Controller
{
	public function sts()
	{
		$conf = config('aliyun_sts');
		$policy = <<<POLICY
{
    "Statement": [
        {
            "Action": [
                "oss:Put*"
            ],
            "Effect": "Allow",
            "Resource": "acs:oss:*:*:hytx-app/*"
        }
    ],
    "Version": "1"
}
POLICY;
		$iClientProfile = DefaultProfile::getProfile("cn-hangzhou", $conf['key'], $conf['secret']);
		$client = new DefaultAcsClient($iClientProfile);
		$request = new AssumeRoleRequest();
		$request->setRoleSessionName("client_name");
		$request->setRoleArn($conf['role_arn']);
		$request->setPolicy($policy);
		$request->setDurationSeconds($conf['expire_time']);
		$response = $client->doAction($request);
		$rows = array();
		$body = $response->getBody();
		$content = json_decode($body);
		if ($response->getStatus() == 200) {
			$rows['StatusCode'] = 200;
			$rows['AccessKeyId'] = $content->Credentials->AccessKeyId;
			$rows['AccessKeySecret'] = $content->Credentials->AccessKeySecret;
			$rows['Expiration'] = $content->Credentials->Expiration;
			$rows['SecurityToken'] = $content->Credentials->SecurityToken;
		} else {
			$rows['StatusCode'] = 500;
			$rows['ErrorCode'] = $content->Code;
			$rows['ErrorMessage'] = $content->Message;
		}
		header('content-type:application/json;charset=utf-8');
		echo json_encode($rows);
		exit;
	}
	public function getSms()
	{

		return SmsMsg::where('mobile_phone','15802302783')
			->select('msg_code')
			->where('type',request('type'))->orderByDesc('id')->first();
	}
	public function banner()
	{
		$data =  \DB::table("app_banner")->select("id", "images", "url")
			->orderBy('sort')->limit(5)->get();

		return $this->renderJson($data);
	}
}
